package com.rainbowelephant.fun.fitness;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class StopWatchActivity extends Activity {

	private final int REFRESH_RATE = 10;
	private Handler mHandler = new Handler();

	private long startTime;
	private long lapStartTime;
	private long elapsedTime;
	private int lapCount = 0;
	private String laps = "";
	private boolean paused = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_watch);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_stop_watch, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

	private void runningButtons() {
		((Button) findViewById(R.id.startButton)).setVisibility(View.GONE);
		((Button) findViewById(R.id.resetButton)).setVisibility(View.GONE);
		((Button) findViewById(R.id.stopButton)).setVisibility(View.VISIBLE);
		((Button) findViewById(R.id.lapButton)).setVisibility(View.VISIBLE);
	}

	private void stoppedButtons() {
		((Button) findViewById(R.id.startButton)).setVisibility(View.VISIBLE);
		((Button) findViewById(R.id.resetButton)).setVisibility(View.VISIBLE);
		((Button) findViewById(R.id.stopButton)).setVisibility(View.GONE);
		((Button) findViewById(R.id.lapButton)).setVisibility(View.GONE);
	}

	public void start(View view) {
		runningButtons();

		if (paused) {
			lapStartTime = System.currentTimeMillis() - elapsedTime;
		}
		else{
			startTime = System.currentTimeMillis();
			lapStartTime = startTime;
			lapCount = 0;
		}
		mHandler.removeCallbacks(startTimer);
		mHandler.postDelayed(startTimer, 0);
	}

	public void reset(View view) {
		paused = false;
		
    	((TextView) findViewById(R.id.timer)).setText("00:00:00.00");
    	lapCount = 0;
    	laps = "";
    	((TextView)findViewById(R.id.laps)).setText(laps);
	}

	public void stop(View view) {
		stoppedButtons();

		mHandler.removeCallbacks(startTimer);
    	paused = true;
	}

	public void lap(View view) {
		lapCount++;
		
		if (lapCount >= 100) return;

		if (lapCount == 1) {
			laps = "Lap #" + lapCount + " - " + 
					((TextView)findViewById(R.id.timer)).getText().toString();
			((TextView)findViewById(R.id.laps)).setText(laps);
		}
		else {
			laps += "\nLap #" + lapCount + " - " + 
					((TextView)findViewById(R.id.timer)).getText().toString();
			((TextView)findViewById(R.id.laps)).setText(laps);
		}
		
		lapStartTime = System.currentTimeMillis();
	}

	private Runnable startTimer = new Runnable() {
		public void run() {
			elapsedTime = System.currentTimeMillis() - lapStartTime;
			updateTimer(elapsedTime);
			mHandler.postDelayed(this,REFRESH_RATE);
		}
	};

	private void updateTimer (long time) {
		int secs = (int) (time/1000);
		int mins = (int) ((time/1000) / 60);
		int hrs = (int) (((time/1000) / 60) / 60);

		// Calculate seconds
		secs = secs % 60;
		String seconds= String.valueOf(secs);
		if (secs == 0) {
			seconds = "00";
		}
		if (secs < 10 && secs > 0) {
			seconds = "0" + seconds;
		}

		// Calculate minutes
		mins = mins % 60;
		String minutes= String.valueOf(mins);
		if (mins == 0) {
			minutes = "00";
		}
		if (mins < 10 && mins > 0) {
			minutes = "0" + minutes;
		}

		// Calculate hours
		String hours= String.valueOf(hrs);
		if (hrs == 0) {
			hours = "00";
		}
		if (hrs < 10 && hrs > 0) {
			hours = "0" + hours;
		}

		// Calculate milliseconds
		String milliseconds = String.valueOf(time % 1000);
		if (milliseconds.length() == 1) {
			milliseconds = "00";
		}
		if (milliseconds.length() == 2) {
			milliseconds = "0" + milliseconds.substring(0, 1);
		}
		if (milliseconds.length() == 3) {
			milliseconds = milliseconds.substring(0, 2);
		}
		
		// Set the timer
		((TextView)findViewById(R.id.timer)).setText(hours + ":" + minutes + ":" + seconds + "." + milliseconds);
	}
    
}
